{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "impressive-harbor",
   "metadata": {},
   "source": [
    "# Norwegian Organisation Numbers"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "institutional-bangladesh",
   "metadata": {},
   "source": [
    "## Introduction"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "natural-interval",
   "metadata": {},
   "source": [
    "The function `clean_no_orgnr()` cleans a column containing Norwegian organisation number (Orgnr) strings, and standardizes them in a given format. The function `validate_no_orgnr()` validates either a single Orgnr strings, a column of Orgnr strings or a DataFrame of Orgnr strings, returning `True` if the value is valid, and `False` otherwise."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "revised-julian",
   "metadata": {},
   "source": [
    "Orgnr strings can be converted to the following formats via the `output_format` parameter:\n",
    "\n",
    "* `compact`: only number strings without any seperators or whitespace, like \"988077917\"\n",
    "* `standard`: Orgnr strings with proper whitespace in the proper places, like \"988 077 917\"\n",
    "\n",
    "Invalid parsing is handled with the `errors` parameter:\n",
    "\n",
    "* `coerce` (default): invalid parsing will be set to NaN\n",
    "* `ignore`: invalid parsing will return the input\n",
    "* `raise`: invalid parsing will raise an exception\n",
    "\n",
    "The following sections demonstrate the functionality of `clean_no_orgnr()` and `validate_no_orgnr()`. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "continuous-index",
   "metadata": {},
   "source": [
    "### An example dataset containing Orgnr strings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "complex-forge",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "df = pd.DataFrame(\n",
    "    {\n",
    "        \"orgnr\": [\n",
    "            \"988077917\",\n",
    "            \"988 077 918\",\n",
    "            \"51824753556\",\n",
    "            \"51 824 753 556\",\n",
    "            \"hello\",\n",
    "            np.nan,\n",
    "            \"NULL\"\n",
    "        ], \n",
    "        \"address\": [\n",
    "            \"123 Pine Ave.\",\n",
    "            \"main st\",\n",
    "            \"1234 west main heights 57033\",\n",
    "            \"apt 1 789 s maple rd manhattan\",\n",
    "            \"robie house, 789 north main street\",\n",
    "            \"(staples center) 1111 S Figueroa St, Los Angeles\",\n",
    "            \"hello\",\n",
    "        ]\n",
    "    }\n",
    ")\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "strong-catch",
   "metadata": {},
   "source": [
    "## 1. Default `clean_no_orgnr`\n",
    "\n",
    "By default, `clean_no_orgnr` will clean orgnr strings and output them in the standard format with proper separators."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "geographic-plasma",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "from dataprep.clean import clean_no_orgnr\n",
    "clean_no_orgnr(df, column = \"orgnr\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "behavioral-hostel",
   "metadata": {},
   "source": [
    "## 2. Output formats"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "municipal-estate",
   "metadata": {},
   "source": [
    "This section demonstrates the output parameter."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "rising-stage",
   "metadata": {},
   "source": [
    "### `standard` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "humanitarian-mechanics",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_no_orgnr(df, column = \"orgnr\", output_format=\"standard\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "muslim-browser",
   "metadata": {},
   "source": [
    "### `compact`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "wrong-separation",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_no_orgnr(df, column = \"orgnr\", output_format=\"compact\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "lonely-fleece",
   "metadata": {},
   "source": [
    "## 3. `inplace` parameter\n",
    "\n",
    "This deletes the given column from the returned DataFrame. \n",
    "A new column containing cleaned Orgnr strings is added with a title in the format `\"{original title}_clean\"`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "coral-proportion",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_no_orgnr(df, column=\"orgnr\", inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "copyrighted-bundle",
   "metadata": {},
   "source": [
    "## 4. `errors` parameter"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bigger-exposure",
   "metadata": {},
   "source": [
    "### `coerce` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "concerned-melbourne",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_no_orgnr(df, \"orgnr\", errors=\"coerce\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "reserved-yemen",
   "metadata": {},
   "source": [
    "### `ignore`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "optimum-grant",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_no_orgnr(df, \"orgnr\", errors=\"ignore\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "czech-wallet",
   "metadata": {},
   "source": [
    "## 4. `validate_no_orgnr()`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "vocal-premium",
   "metadata": {},
   "source": [
    "`validate_no_orgnr()` returns `True` when the input is a valid Orgnr. Otherwise it returns `False`.\n",
    "\n",
    "The input of `validate_no_orgnr()` can be a string, a Pandas DataSeries, a Dask DataSeries, a Pandas DataFrame and a dask DataFrame.\n",
    "\n",
    "When the input is a string, a Pandas DataSeries or a Dask DataSeries, user doesn't need to specify a column name to be validated. \n",
    "\n",
    "When the input is a Pandas DataFrame or a dask DataFrame, user can both specify or not specify a column name to be validated. If user specify the column name, `validate_no_orgnr()` only returns the validation result for the specified column. If user doesn't specify the column name, `validate_no_orgnr()` returns the validation result for the whole DataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "accurate-religious",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import validate_no_orgnr\n",
    "print(validate_no_orgnr(\"988077917\"))\n",
    "print(validate_no_orgnr(\"988 077 918\"))\n",
    "print(validate_no_orgnr(\"51824753556\"))\n",
    "print(validate_no_orgnr(\"51 824 753 556\"))\n",
    "print(validate_no_orgnr(\"hello\"))\n",
    "print(validate_no_orgnr(np.nan))\n",
    "print(validate_no_orgnr(\"NULL\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "promotional-infrared",
   "metadata": {},
   "source": [
    "### Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "acquired-battery",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_no_orgnr(df[\"orgnr\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "reserved-liabilities",
   "metadata": {},
   "source": [
    "### DataFrame + Specify Column"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "periodic-weight",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_no_orgnr(df, column=\"orgnr\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "laden-original",
   "metadata": {},
   "source": [
    "### Only DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "several-carbon",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_no_orgnr(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "following-affair",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
